/*
 * Copyright (C) 2010-2011 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package app.timetogo.view;

import java.util.Calendar;

public final class DaysOfWeek {
    private static final int SUNDAY = 64;
    private static final int SATURDAY = 32;
    private static final int FRIDAY = 16;
    private static final int THURSDAY = 8;
    private static final int WEDNESDAY = 4;
    private static final int TUESDAY = 2;
    private static final int MONDAY = 1;
    private int repeat;

    public DaysOfWeek(final int repeat) {
        this.repeat = repeat;
    }

    public boolean[] getRepeatArr() {
        return new boolean[] {
                   (repeat & MONDAY) == MONDAY,
                   (repeat & TUESDAY) == TUESDAY,
                   (repeat & WEDNESDAY) == WEDNESDAY,
                   (repeat & THURSDAY) == THURSDAY,
                   (repeat & FRIDAY) == FRIDAY,
                   (repeat & SATURDAY) == SATURDAY,
                   (repeat & SUNDAY) == SUNDAY,
               };
    }

    public int getRepeat() {
        return repeat;
    }

    public String getRepeatStr() {
        final StringBuilder buff = new StringBuilder();
        boolean addComma = false;

        if ((repeat & MONDAY) == MONDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Mon");
            addComma = true;
        }

        if ((repeat & TUESDAY) == TUESDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Tue");
            addComma = true;
        }

        if ((repeat & WEDNESDAY) == WEDNESDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Wed");
            addComma = true;
        }

        if ((repeat & THURSDAY) == THURSDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Thu");
            addComma = true;
        }

        if ((repeat & FRIDAY) == FRIDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Fri");
            addComma = true;
        }

        if ((repeat & SATURDAY) == SATURDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Sat");
            addComma = true;
        }

        if ((repeat & SUNDAY) == SUNDAY) {
            if (addComma) {
                buff.append(",");
            }

            buff.append("Sun");
            addComma = true;
        }

        return buff.toString();
    }

    public void setRepeat(final boolean[] vals) {
        repeat = 0;

        switch (vals.length) {
            case 7:

                if (vals[6]) {
                    repeat |= SUNDAY;
                }

            case 6:

                if (vals[5]) {
                    repeat |= SATURDAY;
                }

            case 5:

                if (vals[4]) {
                    repeat |= FRIDAY;
                }

            case 4:

                if (vals[3]) {
                    repeat |= THURSDAY;
                }

            case 3:

                if (vals[2]) {
                    repeat |= WEDNESDAY;
                }

            case 2:

                if (vals[1]) {
                    repeat |= TUESDAY;
                }

            case 1:

                if (vals[0]) {
                    repeat |= MONDAY;
                }

            default:
        }
    }

    public boolean isActiveToday() {
        final Calendar now = Calendar.getInstance();
        final int today = now.get(Calendar.DAY_OF_WEEK);

        switch (today) {
            case Calendar.MONDAY:
                return (repeat & MONDAY) == MONDAY;

            case Calendar.TUESDAY:
                return (repeat & TUESDAY) == TUESDAY;

            case Calendar.WEDNESDAY:
                return (repeat & WEDNESDAY) == WEDNESDAY;

            case Calendar.THURSDAY:
                return (repeat & THURSDAY) == THURSDAY;

            case Calendar.FRIDAY:
                return (repeat & FRIDAY) == FRIDAY;

            case Calendar.SATURDAY:
                return (repeat & SATURDAY) == SATURDAY;

            case Calendar.SUNDAY:
                return (repeat & SUNDAY) == SUNDAY;

            default:
                return false;
        }
    }
}
